#include "mysqlconnectionpool.h"

MysqlConnectionPool::MysqlConnectionPool(const std::string &db,
                                         const std::string &host,
                                         const std::string &user,
                                         const std::string &password,
                                         unsigned short port) :
    m_db(db),
    m_server(host),
    m_user(user),
    m_password(password),
    m_port(port)
{

}

MysqlConnectionPool::~MysqlConnectionPool()
{

}

mysqlpp::Connection *MysqlConnectionPool::create()
{
    mysqlpp::Connection *_new = new mysqlpp::Connection();

    if(!_new){
        throw std::bad_alloc();
    }

    _new->set_option(new mysqlpp::ConnectTimeoutOption(20));
    _new->set_option(new mysqlpp::ReconnectOption(true));

    _new->connect(m_db.c_str(),
                  m_server.c_str(),
                  m_user.c_str(),
                  m_password.c_str(),
                  m_port);

    return _new;
}

void MysqlConnectionPool::destroy(mysqlpp::Connection *c)
{
    delete c;
}

unsigned int MysqlConnectionPool::max_idle_time()
{
    return 180;
}
